package org.cloud.common.util;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

import org.cloud.common.exception.SlothCloudException;
import org.springframework.lang.Nullable;

/**
 * 集合工具类
 * 
 * @author wangyuan
 * @since 2018年7月19日
 */
public class CollectionUtils extends org.springframework.util.CollectionUtils {
	
	public static boolean isNotEmpty(@Nullable Collection<?> collection) {
        return !isEmpty(collection);
    }
    
    public static boolean isNotEmpty(@Nullable Map<?, ?> map) {
        return !isEmpty(map);
    }
    
    /**
     * 将集合数据转换成以,分隔的字符串
     * @param collection
     * @return
     */
    public static String collectionToString(Collection<?> collection) {
        if(isEmpty(collection)){
            return null;
        }
        
        Iterator<?> iterator = collection.iterator();
        StringBuilder sb = new StringBuilder();
        boolean sign = true;
        while(iterator.hasNext()){
            if(sign){
                sb.append(iterator.next());
                sign = false;
            } else {
                sb.append(",");
                sb.append(iterator.next());
            }
        }
        
        return sb.toString();
    }
    
    /**
     * 将collection2集合中的元素添加到collection1集合中
     * @param collection1 不能为空
     * @param collection2
     */
    public static <T> Collection<T> addAll(Collection<T> collection1, Collection<T> collection2) {
        if(isEmpty(collection1)){
            throw new SlothCloudException("collection1不能为空");
        }
        
        if(isEmpty(collection2)){
            return collection1;
        }
        
        collection1.addAll(collection2);
        return collection1;
    }
}
